import Vue from "vue";
import Router from "vue-router";
import home from '../views/index/home.vue';
import login from '../views/index/login.vue';
import menuIndex from '../views/index/menuIndex.vue';

import system from  './module/system';//系统路由
import management from './module/management';//管理路由
import course from './module/course';//教程路由
Vue.use(Router);
/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}
// 重组路由模块
function construction() {
  // 所有路由
  let routerModule = [
      ...system,
      ...management,
      ...course,
  ];
  routerModule.forEach(item => {
      item.meta = {};
      const title = "管理后台"
      item.meta.title = item.title || title;
      item.component = () =>
          import(`@/views${item.filePath}.vue`)
  });
  return routerModule;
}
const routes = [
  {
    path: '/',
    name: 'login',
    component: login,
    meta: {
      title: '登录'
    }
  }, {
    path: '/menuIndex',
    name: 'menuIndex',
    component: menuIndex,
    redirect: {
      name: 'home'
    },
    children: [{
      meta: {
        title: '首页'
      },
      path: '/home',
      name: 'home',
      component: home,
    },
    ...construction()
    ]
  },
];

const router = new Router({
  mode: "hash",
  base: process.env.BASE_URL,
  routes
});

export default router;
